package com.fr.data.impl;

import com.fr.base.ArrayUtils;
import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.data.AbstractDict;
import com.fr.data.Dictionary.MV;
import com.fr.report.core.ScriptUtils;
import com.fr.report.script.Calculator;
import com.fr.report.script.CurrentValueNameSpace;
import com.fr.report.script.core.FArray;
import com.fr.report.script.core.parser.UtilEvalError;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class FormulaDictionary extends AbstractDict
{
  public static final String XML_TAG = "FormulaDict";
  public static final String E_XML_TAG = "EFormulaDict";
  private String produceFormula;
  private String excuteFormula;
  private transient List kvs;

  public String getProduceFormula()
  {
    return this.produceFormula;
  }

  public void setProduceFormula(String paramString)
  {
    this.produceFormula = paramString;
  }

  public String getExcuteFormula()
  {
    return this.excuteFormula;
  }

  public void setExcuteFormula(String paramString)
  {
    this.excuteFormula = paramString;
  }

  public String[] getDependence()
  {
    return ((String[])ArrayUtils.addAll(super.getDependence(), ScriptUtils.getDependenceByFormula(this.produceFormula.substring(1))));
  }

  public Iterator entrys(Calculator paramCalculator)
  {
    createKVS(paramCalculator);
    return new EntryIterator(this, null);
  }

  public Object get(Object paramObject, Calculator paramCalculator)
  {
    CurrentValueNameSpace localCurrentValueNameSpace = new CurrentValueNameSpace(paramObject);
    paramCalculator.pushNameSpace(localCurrentValueNameSpace);
    createKVS(paramCalculator);
    paramCalculator.removeNameSpace(localCurrentValueNameSpace);
    int i = 0;
    int j = this.kvs.size();
    while (i < j)
    {
      Dictionary.MV localMV = (Dictionary.MV)this.kvs.get(i);
      if (ComparatorUtils.equals(localMV.getModel(), paramObject))
        return localMV.getView();
      ++i;
    }
    return null;
  }

  public void reset()
  {
    this.kvs = null;
  }

  private void createKVS(Calculator paramCalculator)
  {
    List localList = keyProduce(paramCalculator);
    if (this.kvs == null)
      this.kvs = new ArrayList(localList.size());
    int i = 0;
    int j = localList.size();
    while (i < j)
    {
      Object localObject = localList.get(i);
      if (j > 0)
        paramCalculator.set("$$$", localObject);
      this.kvs.add(new Dictionary.MV(localObject, valueProduce(localObject, paramCalculator)));
      ++i;
    }
  }

  private List keyProduce(Calculator paramCalculator)
  {
    if (paramCalculator == null)
      paramCalculator = Calculator.createCalculator();
    FArray localFArray = null;
    Object localObject = null;
    try
    {
      if (!(StringUtils.isEmpty(this.produceFormula)))
        localObject = paramCalculator.eval(this.produceFormula);
    }
    catch (UtilEvalError localUtilEvalError)
    {
      FRContext.getLogger().log(Level.WARNING, localUtilEvalError.getMessage(), localUtilEvalError);
    }
    if (localObject instanceof FArray)
      localFArray = (FArray)localObject;
    else
      localFArray = new FArray(new Object[] { localObject });
    return localFArray.toList();
  }

  private Object valueProduce(Object paramObject, Calculator paramCalculator)
  {
    Object localObject = paramObject;
    try
    {
      if (!(StringUtils.isEmpty(this.excuteFormula)))
        localObject = paramCalculator.eval(this.excuteFormula);
    }
    catch (UtilEvalError localUtilEvalError)
    {
      FRContext.getLogger().log(Level.WARNING, localUtilEvalError.getMessage(), localUtilEvalError);
    }
    return localObject;
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isChildNode())
      if ("FormulaDict".equals(paramXMLableReader.getTagName()))
        setProduceFormula(paramXMLableReader.getElementValue());
      else if ("EFormulaDict".equals(paramXMLableReader.getTagName()))
        setExcuteFormula(paramXMLableReader.getElementValue());
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    super.writeXML(paramXMLPrintWriter);
    if (!(StringUtils.isEmpty(this.produceFormula)))
      paramXMLPrintWriter.startTAG("FormulaDict").textNode(this.produceFormula).end();
    if (!(StringUtils.isEmpty(this.excuteFormula)))
      paramXMLPrintWriter.startTAG("EFormulaDict").textNode(this.excuteFormula).end();
  }

  public boolean equals(Object paramObject)
  {
    return ((paramObject instanceof FormulaDictionary) && (ComparatorUtils.equals(((FormulaDictionary)paramObject).excuteFormula, this.excuteFormula)) && (ComparatorUtils.equals(((FormulaDictionary)paramObject).produceFormula, this.produceFormula)));
  }

  static List access$100(FormulaDictionary paramFormulaDictionary)
  {
    return paramFormulaDictionary.kvs;
  }

  private class EntryIterator
    implements Iterator
  {
    private int next;
    private final FormulaDictionary this$0;

    private EntryIterator()
    {
      this.this$0 = paramFormulaDictionary;
      this.next = 0;
    }

    public boolean hasNext()
    {
      return (this.next < FormulaDictionary.access$100(this.this$0).size());
    }

    public Object next()
    {
      Object localObject = FormulaDictionary.access$100(this.this$0).get(this.next);
      this.next += 1;
      return localObject;
    }

    public void remove()
    {
    }

    EntryIterator(, FormulaDictionary.1 param1)
    {
      this(paramFormulaDictionary);
    }
  }
}